package top.melody.ecommerce.webapps.www.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import top.melody.ecommerce.common.util.JsonMapper;
import top.melody.ecommerce.platform.api.domain.account.AccountEnterpriseAuth;
import top.melody.ecommerce.platform.handler.enums.ResultMsg;
import top.melody.ecommerce.webapps.www.base.Constant;
import top.melody.ecommerce.webapps.www.weblogic.AuthenticationWeblogic;

import javax.annotation.Resource;
import java.util.Locale;

/**
 * @author: blentle
 * @group: rd
 * @createdate: 2016/11/11 9:55
 * @mail: blentle.huan.ren@gmail.com
 * @description: 商家(生产者资质认证)
 * @since:1.0
 */
@Controller
@RequestMapping("/producer/auth")
public class AuthenticationController {

    private static final Logger logger = LoggerFactory.getLogger(AuthenticationController.class);
    @Resource
    private AuthenticationWeblogic authenticationWeblogic;
    @Resource
    private ResourceBundleMessageSource i18nSource;

    @RequestMapping("/index")
    public String authIndex() {
        return "authentication/authentication-index";
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public String saveAuthInfo(AccountEnterpriseAuth enterprise) {
        ResultMsg resultMsg = new ResultMsg(i18nSource.getMessage(Constant.ERROR_OPERATE_FAILED,null, Locale.CHINA));
        try {
            AccountEnterpriseAuth persist = authenticationWeblogic.createAuthentication(enterprise);
            resultMsg.setMsg(null);
            resultMsg.setStatusCode(ResultMsg.CODE_S);
        } catch (Exception e) {
            logger.error("occurred exception when save authentication information !", e);
        }
        return JsonMapper.jsonMapper().toJson(resultMsg);
    }

    @RequestMapping("/preedit")
    public String preEdit() {
        return "authentication/authentication-edit";
    }

    @RequestMapping("/edit")
    @ResponseBody
    public String edit() {
        ResultMsg resultMsg = new ResultMsg();
        try {
            //todo:
        } catch (Exception e) {
            logger.error("occurred exception when edit authentication information !", e);
        }
        return JsonMapper.jsonMapper().toJson(resultMsg);
    }



}
